package oracle.apps.financials.collection;

import java.util.concurrent.Callable;

import oracle.apps.financials.collection.activity.BaseListActivity;
import oracle.apps.financials.collection.activity.TopDelinquentCustomerActivity;
import oracle.apps.financials.collection.dao.DatabaseHelper;
import oracle.apps.financials.collection.po.Collector;
import oracle.apps.financials.collection.po.Customer;
import oracle.apps.financials.collection.po.MetricsItem;
import oracle.apps.financials.collection.util.MockDataFactory;
import oracle.apps.financials.collection.widget.LoadingDialog;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.j256.ormlite.dao.Dao;

public class LoadDataAsyncTask extends LoadingDialog {
	public static final String TAG = BaseListActivity.class.getSimpleName();

	public static final String namespace = "http://collection.financials.apps.oracle/";

	public static final String serviceURL = "http://adc6260336.us.oracle.com:7101/Collection-Project-context-root/DataPublishServicePort";

	public final String methodName = "getESSBaseCubeData";

	public final int sessionID = 111111;

	public DatabaseHelper databaseHelper;

	private Dao<MetricsItem, Integer> metricsItemDao;

	private MetricsItem metricsItem;
	private Handler mHandler;

	public LoadDataAsyncTask(Activity activity, Handler mHandler, int loadingMsg, int failMsg, DatabaseHelper databaseHelper) {
		super(activity, loadingMsg, failMsg);
		this.databaseHelper = databaseHelper;
		this.setShowDialog(true);
		this.mHandler = mHandler;
	}

	private void loadData(boolean isRebuid) {
		try {
			Dao<Customer, Integer> customerDao = databaseHelper.getCustomerDao();
			Dao<Collector, Integer> collectorDao = databaseHelper.getCollectorDao();
			metricsItemDao = databaseHelper.getMetricsItemDao();
			Log.i(TAG, "----------------------------------------------------");
			long k = metricsItemDao.countOf();

			if (k > 0 && !isRebuid) {
				Log.i(TAG, "Exsiting Metrics Item Number: " + metricsItemDao.countOf() + ", so will not rebuild DB. ");
			} else {
				Log.i(TAG, "Exsiting Metrics Item Number: " + metricsItemDao.countOf());
			}

			if (k < 1 || isRebuid) {
				Log.i(TAG, "----------------------------------------------------");
				Log.i(TAG, "Start to rebuild DB : " + databaseHelper.getDatabaseName());
				databaseHelper.onUpgrade();
				Log.i(TAG, "----------------------------------------------------");
				Log.i(TAG, "Start to load data to DB ");

				for (Customer customer : MockDataFactory.getCustomerList()) {
					customerDao.create(customer);
				}
				metricsItemDao.callBatchTasks(new Callable<Void>() {
					public Void call() throws Exception {

						int count = 0;
						for (MetricsItem item : MockDataFactory.getMetricsItemList()) {

							metricsItem = item;
							if (count % 100 == 99) {
								Log.i(TAG, "Loaded Metrics Item Number: " + metricsItemDao.countOf());
							}
							metricsItemDao.create(item);
							count++;
						}
						return null;

					}
				});

				Log.i(TAG, "----------------------------------------------------");
				Log.i(TAG, "Loaded Customer Number: " + customerDao.countOf());
				Log.i(TAG, "Loaded Collector Number: " + collectorDao.countOf());
				Log.i(TAG, "Loaded Metrics Item Number: " + metricsItemDao.countOf());
				Log.i(TAG, "----------------------------------------------------");
			}

		} catch (Exception e) {
			if (metricsItem != null) {
				Log.e(TAG, metricsItem.getCustomer().getCustomerName() + " " + metricsItem.getItemTime());
			}
			Log.e(TAG, e.getMessage());
			e.printStackTrace();
		}
	}

	@Override
	public Object doInBackground(Object... argument) {
		loadData(argument != null && "Rebuild".equalsIgnoreCase(String.valueOf(argument)));
		return "";
	}

	@Override
	public void doStuffWithResult(Object result) {
		if (mHandler != null) {
			Message message = new Message();
			message.what = 1;
			message.obj = null;
			mHandler.sendMessage(message);
		}

	}

}
